package com.li.toolkit.utils;

import com.li.toolkit.constant.NumConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 文件操作工具类
 *
 * @author LiDong
 * @version 1.0.0
 * @createTime 2/19/2022 10:12 AM
 */
public final class FileUtils {


    private FileUtils() {
    }

    private static final Logger logger = LoggerFactory.getLogger(String.valueOf(FileUtils.class));

    /**
     * 读文件为 String
     *
     * @param file 文件
     * @return String
     * @throws IOException
     */
    public static String toStringByRandomAccessFile(File file) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        StringBuilder stringBuffer = new StringBuilder();
        byte[] bit = new byte[NumConst.ONE_BYTE_SIZE];
        /**
         while ((hasRead=randomAccessFile.read(bit))>0) {
         stringBuffer.append(new String(bit, 0, hasRead));
         }*/
        while (true) {
            int read = randomAccessFile.read(bit);
            if (-1 == read) break;
            stringBuffer.append(new String(bit, StandardCharsets.UTF_8));
        }
        randomAccessFile.close();
        return String.valueOf(stringBuffer);
    }

    /**
     * 文件尾部追加内容
     *
     * @param file 文件
     * @param str  需要追加的字符串
     * @throws IOException
     */
    public static void writeByRandomAccessFile(File file, String str) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
        // 记录文件长度,设置从文件末尾处写入内容
        randomAccessFile.seek(randomAccessFile.length());
        randomAccessFile.write(str.getBytes(StandardCharsets.UTF_8));
        randomAccessFile.close();
    }

    /**
     * 读取文件中的内容：字符流
     *
     * @param file 文件
     * @return String
     * @throws IOException
     */
    public static String readByFileReader(File file) throws IOException {
        StringBuilder stringBuffer = new StringBuilder();
        // 创建FileReader对象
        FileReader fileReader = new FileReader(file);
        // 读取位置
        int head;
        while (true) {
            int read = fileReader.read();
            if (-1 == (head = read)) {
                break;
            }
            // 将读取的内容强制转换为char类型
            stringBuffer.append((char) head);
        }
        fileReader.close();
        return String.valueOf(stringBuffer);
    }

    /**
     * 字符流方式写文件
     *
     * @param file 文件
     * @param str  需要写的字符串
     * @throws IOException
     */
    public static void writeByFileWriter(File file, String str) throws IOException {
        // 追加的方式写入内容
        FileWriter fileWriter = new FileWriter(file, true);
        fileWriter.write(str);
        fileWriter.close();
    }

    /**
     * 字符缓冲区输入流读文件
     *
     * @param file 文件
     * @return String
     * @throws IOException
     */
    public static String readByBufferedReader(File file) throws IOException {
        StringBuilder stringBuffer = new StringBuilder();
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String strLine;
        while (true) {
            String readLine = bufferedReader.readLine();
            if (null == (strLine = readLine)) {
                break;
            }
            stringBuffer.append(strLine);
        }
        fileReader.close();
        bufferedReader.close();
        return String.valueOf(stringBuffer);
    }


    /**
     * FileInputStream 读文件
     *
     * @param file 文件
     * @return String
     * @throws IOException
     */
    public static String readByFileInputStream(File file) throws IOException {
        StringBuilder stringBuffer = new StringBuilder();
        //创建流对象
        FileInputStream fileInputStream = new FileInputStream(file);
        int data;
        // 可读取的字节数
        logger.info("可读取的字节数：{}", fileInputStream.available());
        // 循环读取内容
        while (true) {
            int read = fileInputStream.read();
            if (-1 == (data = read)) {
                break;
            }
            stringBuffer.append((char) data);
        }
        // 关闭流对象
        fileInputStream.close();
        return String.valueOf(stringBuffer);
    }

    /**
     * FileOutputStream 写文件
     *
     * @param file 文件
     * @param str  字符串
     * @throws IOException
     */
    public static void writeByFileOutPutStream(File file, String str) throws IOException {
        // 放到字节数组里
        byte[] words = str.getBytes();
        FileOutputStream fileOutPutStream = new FileOutputStream(file, true);
        // 写入文件
        fileOutPutStream.write(words, 0, words.length);
        logger.info("文件已更新");
        // 关闭流
        fileOutPutStream.close();
    }


}
